"""
This code is currently broken

small changes are required to adapt it to the new pulsed measurements (without hardware API).

See rabi.py as example.
"""

class T1( PulsedTau ):
    
    """Defines a T1 measurement."""

    laser       = Range(low=1., high=100000.,   value=3000.,    desc='laser [ns]',      label='laser [ns]',       mode='text', auto_set=False, enter_set=True)
        
    def generate_sequence(self):
        tau = self.tau
        laser = self.laser
        sequence = [ (['aom'],laser) ]
        for t in tau:
            sequence += [  ([],t),  (['detect','aom'],laser)  ]
        sequence += [  ([],1000)  ]
        sequence += [  (['sequence'],100)  ]
        return sequence

    get_set_items = PulsedTau.get_set_items + ['laser']

    traits_view = View(VGroup(HGroup(Item('submit_button',   show_label=False),
                                     Item('remove_button',   show_label=False),
                                     Item('resubmit_button', show_label=False),
                                     Item('priority'),
                                     Item('state', style='readonly'),
                                     Item('run_time', style='readonly',format_str='%.f'),
                                     Item('stop_time'),
                                     ),
                              Tabbed(VGroup(HGroup(Item('tau_begin',     width=-80, enabled_when='state != "run"'),
                                                   Item('tau_end',       width=-80, enabled_when='state != "run"'),
                                                   Item('tau_delta',     width=-80, enabled_when='state != "run"'),
                                                   ),
                                            label='parameter'),
                                     VGroup(HGroup(Item('laser',         width=-80, enabled_when='state != "run"'),
                                                   Item('record_length', width=-80, enabled_when='state != "run"'),
                                                   Item('bin_width',     width=-80, enabled_when='state != "run"'),),
                                            label='settings'),
                              ),
                       ),
                       title='T1 Measurement',
                       )

class SingletDecay( Pulsed ):
    
    """Singlet Decay."""

    laser       = Range(low=1., high=100000.,   value=3000.,    desc='laser [ns]',      label='laser [ns]',       mode='text', auto_set=False, enter_set=True)

    tau_begin   = Range(low=0., high=1e8,       value=0.,     desc='tau begin [ns]',  label='tau begin [ns]',   mode='text', auto_set=False, enter_set=True)
    tau_end     = Range(low=1., high=1e8,       value=300.,     desc='tau end [ns]',    label='tau end [ns]',     mode='text', auto_set=False, enter_set=True)
    tau_delta   = Range(low=1., high=1e6,       value=3.,      desc='delta tau [ns]',  label='delta tau [ns]',   mode='text', auto_set=False, enter_set=True)

    tau = Array( value=np.array((0.,1.)) )

    def apply_parameters(self):
        """Overwrites apply_parameters() from pulsed. Prior to generating sequence, etc., generate the tau mesh."""
        self.tau = np.arange(self.tau_begin, self.tau_end, self.tau_delta)
        Pulsed.apply_parameters(self)

    def start_up(self):
        PulseGenerator().Night()

    def shut_down(self):
        PulseGenerator().Light()

    def generate_sequence(self):

        tau = self.tau
        laser = self.laser

        sequence = [ (['sequence'],100) ]
        for t in tau:
            sequence += [  ([],t),  (['detect','aom'],laser)  ]
        return sequence

    get_set_items = Pulsed.get_set_items + ['tau_begin','tau_end','tau_delta','laser','tau']

    traits_view = View(VGroup(HGroup(Item('submit_button',   show_label=False),
                                     Item('remove_button',   show_label=False),
                                     Item('resubmit_button', show_label=False),
                                     Item('priority'),
                                     Item('state', style='readonly'),
                                     Item('run_time', style='readonly',format_str='%.f'),
                                     ),
                              Tabbed(VGroup(HGroup(Item('laser',         width=-80, enabled_when='state != "run"'),),
                                            HGroup(Item('tau_begin',     width=-80, enabled_when='state != "run"'),
                                                   Item('tau_end',       width=-80, enabled_when='state != "run"'),
                                                   Item('tau_delta',     width=-80, enabled_when='state != "run"'),
                                                   Item('stop_time'),),
                                            label='manipulation'),
                                     VGroup(HGroup(Item('record_length', width=-80, enabled_when='state != "run"'),
                                                   Item('bin_width',     width=-80, enabled_when='state != "run"'),),
                                            label='detection'
                                            ),
                                     ),
                              ),
                              title='Singlet decay',
                              )
    
class SingletDecayDouble( PulsedTau ):
    
    """Singlet Decay."""

    laser       = Range(low=1., high=100000.,   value=3000.,    desc='laser [ns]',      label='laser [ns]',       mode='text', auto_set=False, enter_set=True)

    def start_up(self):
        PulseGenerator().Night()

    def shut_down(self):
        PulseGenerator().Light()

    def generate_sequence(self):

        tau = self.tau
        laser = self.laser

        sequence = [ (['sequence'],100) ]
        for t in tau:
            sequence += [  ([],t),  (['detect','aom'],laser)  ]
        for t in tau[::-1]:
            sequence += [  ([],t),  (['detect','aom'],laser)  ]
        return sequence

    get_set_items = Pulsed.get_set_items + ['tau_begin','tau_end','tau_delta','laser','tau']

    traits_view = View(VGroup(HGroup(Item('submit_button',   show_label=False),
                                     Item('remove_button',   show_label=False),
                                     Item('resubmit_button', show_label=False),
                                     Item('priority'),
                                     Item('state', style='readonly'),
                                     Item('run_time', style='readonly',format_str='%.f'),
                                     Item('stop_time'),
                                     ),
                              Tabbed(VGroup(HGroup(Item('laser',         width=-80, enabled_when='state != "run"'),),
                                            HGroup(Item('tau_begin',     width=-80, enabled_when='state != "run"'),
                                                   Item('tau_end',       width=-80, enabled_when='state != "run"'),
                                                   Item('tau_delta',     width=-80, enabled_when='state != "run"'),
                                                   ),
                                            label='manipulation'),
                                     VGroup(HGroup(Item('record_length', width=-80, enabled_when='state != "run"'),
                                                   Item('bin_width',     width=-80, enabled_when='state != "run"'),),
                                            label='detection'
                                            ),
                                     ),
                              ),
                              title='Singlet decay double',
                              )
    


class T1pi( Rabi ):
    
    """Defines a T1 measurement with pi pulse."""

    t_pi        = Range(low=1., high=100000.,   value=1000.,    desc='pi pulse length',     label='pi [ns]',      mode='text', auto_set=False, enter_set=True)

    def generate_sequence(self):
        tau = self.tau
        laser = self.laser
        wait  = self.wait
        t_pi   = self.t_pi
        sequence = []
        for t in tau:
            sequence.append(  ([       ],   t )  )
            sequence.append(  (['detect','aom'],  laser  )  )
            sequence.append(  ([       ],  wait   )  )
        for t in tau:
            sequence.append(  (['mw'   ],  t_pi   )  )
            sequence.append(  ([       ],   t )  )
            sequence.append(  (['detect','aom'],  laser  )  )
            sequence.append(  ([       ],  wait   )  )
        sequence.append(  (['sequence'], 100  )  )
        return sequence

    traits_view = View(VGroup(HGroup(Item('submit_button',   show_label=False),
                                     Item('remove_button',   show_label=False),
                                     Item('resubmit_button', show_label=False),
                                     Item('priority'),
                                     Item('state', style='readonly'),
                                     Item('run_time', style='readonly',format_str='%.f'),
                                     Item('stop_time'),
                                     ),
                              Tabbed(VGroup(HGroup(Item('frequency',     width=-80, enabled_when='state != "run"'),
                                                   Item('power',         width=-80, enabled_when='state != "run"'),                                                
                                                   Item('t_pi',          width=-80, enabled_when='state != "run"'),),                                                      
                                            HGroup(Item('tau_begin',     width=-80, enabled_when='state != "run"'),
                                                   Item('tau_end',       width=-80, enabled_when='state != "run"'),
                                                   Item('tau_delta',     width=-80, enabled_when='state != "run"'),
                                                   ),
                                            label='parameter'),
                                     VGroup(HGroup(Item('laser',         width=-80, enabled_when='state != "run"'),
                                                   Item('wait',          width=-80, enabled_when='state != "run"'),
                                                   Item('record_length', width=-80, enabled_when='state != "run"'),
                                                   Item('bin_width',     width=-80, enabled_when='state != "run"'),),
                                            label='settings'),
                                     ),
                              ),
                       title='T1pi',
                       )
    
    get_set_items = Rabi.get_set_items + ['t_pi']

class T1Meta( Rabi ):
    
    """T1 measurement of metastable state."""

    t_pi        = Range(low=0., high=100000.,   value=1000.,    desc='pi pulse length',     label='pi [ns]',      mode='text', auto_set=False, enter_set=True)
    delay       = Range(low=0., high=1000., value=450.,    desc='delay of AOM relative to trigger',     label='AOM delay [ns]',      mode='text', auto_set=False, enter_set=True)

    def generate_sequence(self):
        tau = self.tau
        laser = self.laser
        wait  = self.wait
        t_pi  = self.t_pi
        delay = self.delay
        s1 = []
        s2 = []
        s2.append( ([],  delay+wait  ) )
        for t in tau:
            s1.append(  ([       ],  wait + t_pi + t  )  )
            s1.append(  (['detect','aom'],  laser      )  )
            s2.append(  (['mw'   ],  t_pi             )  )
            s2.append(  ([       ],  t+laser+wait     )  )
        s1.append(  ([          ], 1000  )  )
        s1.append(  (['sequence'], 100   )  )
        s2.pop()
        """
        s1.append( (['aom'],  laser   ) )
        s2.append( ([],  delay+laser+wait  ) )
        for t in tau:
            s1.append(  ([       ],  wait + t_pi + t  )  )
            s1.append(  (['detect','aom'],  laser      )  )
            s2.append(  (['mw'   ],  t_pi             )  )
            s2.append(  ([       ],  t+laser+wait     )  )
        s1.append(  ([          ], 1000  )  )
        s1.append(  (['sequence'], 100   )  )
        s2.pop()
        """
        return sequence_union(s1,s2)

    get_set_items = Rabi.get_set_items + ['t_pi','delay']

    traits_view = View(VGroup(HGroup(Item('submit_button',   show_label=False),
                                     Item('remove_button',   show_label=False),
                                     Item('resubmit_button', show_label=False),
                                     Item('priority'),
                                     Item('state', style='readonly'),
                                     Item('run_time', style='readonly',format_str='%.f'),
                                     Item('stop_time'),
                                     ),
                              Tabbed(VGroup(HGroup(Item('frequency',     width=-80, enabled_when='state != "run"'),
                                                   Item('power',         width=-80, enabled_when='state != "run"'),                                                
                                                   Item('t_pi',          width=-80, enabled_when='state != "run"'),),
                                            HGroup(Item('tau_begin',     width=-80, enabled_when='state != "run"'),
                                                   Item('tau_end',       width=-80, enabled_when='state != "run"'),
                                                   Item('tau_delta',     width=-80, enabled_when='state != "run"'),
                                                   ),
                                            label='parameter'),
                                     VGroup(HGroup(Item('laser',         width=-80, enabled_when='state != "run"'),
                                                   Item('wait',          width=-80, enabled_when='state != "run"'),
                                                   Item('delay',         width=-80, enabled_when='state != "run"'),
                                                   Item('record_length', width=-80, enabled_when='state != "run"'),
                                                   Item('bin_width',     width=-80, enabled_when='state != "run"'),),
                                            label='settings'),
                                     ),
                              ),
                       title='T1Meta',
                       )
